// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var validate = require("validate");
	var css_product = require("./constractSupervisionDetail.css");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var productId;
	var init = function(page) {
		productId = page.params.productId;
		initEvent();
		inquireFinanceProductDetail(productId);
		inquireFinanceProductExpensePageList(productId);
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#supervisionDetailEdit").unbind("click").click(function() {
			router.route("constractSupervisionManage/constractSupervisionAdd.html?productId=" + productId);
		});

		//返回物流合同
		$(".constractSupervisionManage").unbind("click").click(function() {
			router.route("constractSupervisionManage/constractSupervisionManage.html");
		});
	};



	//查询合同明细
	var inquireFinanceProductDetail = function(productId) {
		base.fnAjax({
			action: "inquireFinanceProductDetail.json",
			oData: {
				productId: productId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					databind.fillForm($("#constractSupervisionDetail"), data.datas[0]);
					var fs = $("#supervisionDetailIntroUuid").val().split(",");
					for (var j = 0; j < fs.length; j++) {
						if ($.trim(fs[j]) != "") {
							$("#constractSupervisionDetail .fs").append('<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + fs[j] + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + fs[j] + '" class="showProductIntro">产品附件</a></div>');
						}
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//删除产品费用
	var deleteFinanceProductExpense = function(productExpenseId) {
		base.fnAjax({
			action: "deleteFinanceProductExpense.json",
			oData: {
				productExpenseId: productExpenseId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					inquireFinanceProductExpensePageList(productId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//查询产品费用列表
	var inquireFinanceProductExpensePageList = function(productId) {
		base.fnAjax({
			action: "inquireFinanceProductExpensePageList.json",
			oData: {
				productId: productId,
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl3 = $("#supervisionListTpl3").html();
					var html3 = juicer(tpl3, data);
					$("#supervisionListBody3").html(html3);

					$(".chargeDetailDelete").unbind("click").click(function() {
						deleteFinanceProductExpense($(this).parents("tr").attr("productexpenseId"));
						inquireFinanceProductExpensePageList(productId);
					});
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#supervisionDetailPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceProductExpensePageList(productId);
						});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});